package com.css.fxfzaqbz.modules.assess2.service.impl;

import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.repository.AttachmentInfoRepository;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.KeyplaceRiskassessment;
import com.css.fxfzaqbz.modules.assess2.repository.KeyplaceRiskassessmentRepository;
import com.css.fxfzaqbz.modules.assess2.service.KeyplaceRiskassessmentService;
import com.css.fxfzaqbz.modules.data.entity.AidedAnalyse;
import com.css.fxfzaqbz.modules.data.vo.AidedAnalyseVo;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KeyplaceRiskassessmentServiceImpl implements KeyplaceRiskassessmentService {

    @Resource
    private KeyplaceRiskassessmentRepository keyplaceRiskassessmentRepository;
    @Resource
    private SUserService sUserService;
    @Resource
    private AttachmentInfoRepository attachmentInfoRepository;


    @Override
    public Map<String, Object> getKeyplaceRiskassessmentById(String id) {
        KeyplaceRiskassessment keyplaceRiskassessment = keyplaceRiskassessmentRepository.getKeyplaceRiskassessmentById(id);
        List<String> strings = new ArrayList<>();
        if (!PlatformObjectUtils.isEmpty(keyplaceRiskassessment.getDescription())) {
            String[] split = keyplaceRiskassessment.getDescription().split("&&");
            for (String s : split) {
                strings.add(s);
            }
            keyplaceRiskassessment.setDescriptions(strings);
        }
        AttachmentInfoEntity attach = attachmentInfoRepository.getAttach(keyplaceRiskassessment.getPictureId());

        Map<String, Object> map = new HashMap();
        map.put("keyplaceRiskassessment", keyplaceRiskassessment);
        map.put("attach", attach);
        return map;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savekeyplaceRiskassessment(KeyplaceRiskassessment keyplaceRiskassessment) {
        keyplaceRiskassessment.setKrId(UUIDGenerator.getUUID());
        keyplaceRiskassessment.setDelFlag(YNEnum.N.toString());
        keyplaceRiskassessment.setCreateUser(PlatformSessionUtils.getUserId());
        keyplaceRiskassessment.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        keyplaceRiskassessment.setKrDate(PlatformDateUtils.getCurrentTimestamp());
        keyplaceRiskassessment.setTypes("重点场所风险评估");
        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        if (sUser != null) {
            keyplaceRiskassessment.setKrPeople(sUser.getUserName());
        }
        keyplaceRiskassessment.setStatus(YNEnum.N.toString());
        keyplaceRiskassessmentRepository.savekeyplaceRiskassessment(keyplaceRiskassessment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteKeyplaceRiskassessment(String id) {
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        String userId = PlatformSessionUtils.getUserId();
        keyplaceRiskassessmentRepository.deleteKeyplaceRiskassessment(id, userId, currentTimestamp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateKeyplaceRiskassessment(KeyplaceRiskassessment keyplaceRiskassessment) {
        keyplaceRiskassessment.setUpdateUser(PlatformSessionUtils.getUserId());
        keyplaceRiskassessment.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        keyplaceRiskassessmentRepository.updateKeyplaceRiskassessment(keyplaceRiskassessment);
    }

    @Override
    public List<KeyplaceRiskassessment> getByIaId(String activeId) {
        return keyplaceRiskassessmentRepository.getByIaId(activeId);
    }

    @Override
    public List<KeyplaceRiskassessment> getKeyplaceRiskassessmentByIaId(String activeId) {
        List<KeyplaceRiskassessment> keyplaceRiskassessmentByIaId = keyplaceRiskassessmentRepository.getKeyplaceRiskassessmentByIaId(activeId);
        for (KeyplaceRiskassessment keyplaceRiskassessment : keyplaceRiskassessmentByIaId) {
            List<String> strings = new ArrayList<>();
            if (!PlatformObjectUtils.isEmpty(keyplaceRiskassessment.getDescription())) {
                String[] split = keyplaceRiskassessment.getDescription().split("&&");
                for (String s : split) {
                    strings.add(s);
                }
                keyplaceRiskassessment.setDescriptions(strings);
            }
        }
        return keyplaceRiskassessmentByIaId;
    }

    @Override
    public boolean check(KeyplaceRiskassessment keyplaceRiskassessment) {
        List<KeyplaceRiskassessment> keyplaceRiskassessments = keyplaceRiskassessmentRepository.check(keyplaceRiskassessment);
        if (keyplaceRiskassessments == null || keyplaceRiskassessments.size() == 0) {
            return false;
        }
        return true;
    }

    @Override
    public void submitResult(String id) {
        KeyplaceRiskassessment keyplaceRiskassessment = keyplaceRiskassessmentRepository.getKeyplaceRiskassessmentById(id);
        List<KeyplaceRiskassessment> keyplaceRiskassessments = keyplaceRiskassessmentRepository.getByEntity(keyplaceRiskassessment);
        String description = "";
        if (keyplaceRiskassessments != null && keyplaceRiskassessments.size() > 0) {
            description = keyplaceRiskassessments.get(0).getDescription();
        }
        keyplaceRiskassessmentRepository.submitResult(id, description);
    }

    @Override
    public void removeResult(String id) {
        String description = "";
        keyplaceRiskassessmentRepository.removeResult(id, description);
    }

    @Override
    public void finalSubmission(List<String> ids) {
        String user = sUserService.getSUser(PlatformSessionContext.getUserID());
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        String userName = sUser.getUserName();
        String mobilePhone = sUser.getMobilePhone();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        keyplaceRiskassessmentRepository.finalSubmission(ids, userName, mobilePhone,currentTimestamp);
    }

    @Override
    public void saveDescription(AidedAnalyseVo aidedAnalyseVo) {
        List<String> descriptions = aidedAnalyseVo.getDescriptions();
        String description = StringUtils.join(descriptions, "&&");
        keyplaceRiskassessmentRepository.saveDescription(aidedAnalyseVo.getIds(), description);
    }

    @Override
    public List<KeyplaceRiskassessment> getByIaIdAndContent(String iaId, String zdcsfxpg) {
        return keyplaceRiskassessmentRepository.getByIaIdAndContent(iaId,zdcsfxpg);
    }


}
